<?php

class EmptyBlocForm extends BaseBlocForm
{

    const WRAPPER_NAME = 'scara';

    public function addEmptyScaraForm($names)
    {
        $names = (array) $names;
        $wrapperForm = @$this->embeddedForms[self::WRAPPER_NAME];
        if ($wrapperForm === null)
        {
            $wrapperForm = new sfForm();
            $this->embedForm(self::WRAPPER_NAME, $wrapperForm);
        }
        foreach ($names as $name)
        {
            $wrapperForm->embedForm($name, new EmptyScaraForm(null, array('bloc_object' => $this->getObject())));
        }

        $this->embedForm(self::WRAPPER_NAME, $wrapperForm);
    }

    public function configure()
    {
        $this->useFields(array('name'));

        // se adauga la validatorul existent o noua conditie
        $nameValidator = $this->getValidator('name');
        $nameValidator = new sfValidatorAnd(array(
                    $nameValidator,
                    new NoSpacesValidator()
                ));
        $nameValidator->setOption('required', false);
        $this->setValidator('name', $nameValidator);

        // numarul de scari afisate pentru fiecare bloc
        $scara = $this->getOption('scara', sfConfig::get('mod_apartamente_scara_per_empty_bloc'));
        $this->addEmptyScaraForm(range(0, $scara - 1));
    }

    protected function doUpdateObject($values = null)
    {
        $values['user_id'] = sfContext::getInstance()->getUser()->getId();
        parent::doUpdateObject($values);
    }

}
